package com.example.project.utils.html;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.druid.support.json.JSONUtils;
import com.example.project.utils.StringUtils;

/**
 * html相关工具类
 * 
 * @author <a href="mailto:xieliqi@chaoxing.com">xieliqi</a>
 * @version 2014-1-17
 */
public class HtmlUtils {
	
	private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式

	private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式

	private static final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式

	private static final String regEx_space = "\\s*|\t|\r|\n";// 定义空格回车换行符

	private static final String reg_br = "<br\\s*/?>";

	private static final String reg_p = "\\<p>|</p>";

	public static String getTextFromHtml(String htmlStr) {
		htmlStr = htmlStr.replaceAll(reg_br, "☆");
		htmlStr = htmlStr.replaceAll(reg_p, "☆");
		htmlStr = delHTMLTag(htmlStr);
		htmlStr = htmlStr.replaceAll("[☆]+", "☆");
		htmlStr = htmlStr.replaceAll(" ", "");
		return htmlStr;
	}

	/**
	 * @param htmlStr
	 * @return 删除Html标签
	 */
	public static String delHTMLTag(String htmlStr) {
		if (StringUtils.isNotBlank(htmlStr)) {
			Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
			Matcher m_script = p_script.matcher(htmlStr);
			htmlStr = m_script.replaceAll(""); // 过滤script标签

			Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
			Matcher m_style = p_style.matcher(htmlStr);
			htmlStr = m_style.replaceAll(""); // 过滤style标签

			Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
			Matcher m_html = p_html.matcher(htmlStr);
			htmlStr = m_html.replaceAll(""); // 过滤html标签

			Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);
			Matcher m_space = p_space.matcher(htmlStr);
			htmlStr = m_space.replaceAll(""); // 过滤空格回车标签

			htmlStr = htmlStr.replaceAll("&nbsp;", ",");// 过滤掉字符空格
		}
		return htmlStr.trim(); // 返回文本字符串
	}

	/**
	 * 换行符转html换行标签
	 * 
	 * @param content
	 * @return
	 */
	public static String nl2brStr(String content) {
		return htmlEscape(content);
	}

	public static String htmlEscape(String input) {
		return org.springframework.web.util.HtmlUtils.htmlEscape(input).replace("\n", "<br/>");
	}

	public static String replaceHtml(String input) {
		if (input == null || input.trim().equals("")) {
			return "";
		}
		// 去掉所有html元素,
		String str = input.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll("<[^>]*>", "");
		str = str.replaceAll("[(/>)<]", "");
		str = str.replaceAll("&nbsp;", " ");
		return str;
	}

	public static void main(String[] args) {
		System.out.println("☆北京世纪读秀技术有限公司☆☆☆☆北京世纪超星信息技术发展有限责任公司☆☆北京世纪读秀技术有限公司☆☆☆☆☆☆☆☆".replaceAll("[☆]+", "☆"));

	}

	/**
	 * 输出json字符串
	 * @param object
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public static void writeJson(Object object, HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json");
		PrintWriter writer = response.getWriter();

		writer.write(JSONUtils.toJSONString(object));
		writer.flush();
		writer.close();

	}

	/**
	 * html转pdf
	 * @param result 文件所在文件夹未赋权状态，1进行赋权；0不进行赋权
	 * @result说明：如果需要for循环执行该方法，则在for循环外对文件路径进行赋权，调用chmod方法
	 * @param filePath 文件所在文件夹
	 * @param urls 转pdf的html页面地址
	 * @param fileTitle 文件名称
	 * @param orientation 横版还是竖版，默认0是竖版,1横版
	 * @return
	 */
	public static int htmltopdf(int result, String filePath, String urls, String fileName, int orientation) {
		try {
			if (StringUtils.isNotEmpty(urls)) {
				// 文件所在文件夹未赋权或赋权失败重新赋权
				if (result != 0) {
					// ##
					result = chmod(filePath);
					// ##
				}
				if (orientation == 1) {
					String command1 = "/opt/rose/fanyajwproject/web/wkhtmltox/bin/wkhtmltopdf --javascript-delay 2000 --orientation Landscape '" + urls + "' "
							+ filePath + fileName;

					result = Runtime.getRuntime().exec(command1).waitFor();
				}
				else {
					String command1 = "/opt/rose/fanyajwproject/web/wkhtmltox/bin/wkhtmltopdf --javascript-delay 2000 '" + urls + "' " + filePath + fileName;

					result = Runtime.getRuntime().exec(command1).waitFor();
				}
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 文件路径赋权
	 * @param filePath
	 * @return
	 */
	public static int chmod(String filePath) {
		int result = 0;
		try {
			String command = "chmod 777 " + filePath;
			result = Runtime.getRuntime().exec(command).waitFor();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}
